package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.SampleDevAttachedSheetEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.trade.SampleDevAttachedSheetRepository;
import cn.com.huzo.repository.trade.SampleDevAttachedSheetPictureRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.trade.SampleDevAttachedSheetManager;
import huzofw.common.util.annotations.WriteLog;
import huzofw.common.util.file.DeleteDirectoryAndFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 开发样品跟单表管理服务对象类实现。
 *
 * @author 范学春
 */
@Service("SampleDevAttachedSheetManager")
public class SampleDevAttachedSheetManagerImpl extends GenericManagerImpl<SampleDevAttachedSheetEntity, Long>
        implements SampleDevAttachedSheetManager {

    private final String uploadPath = "/opt/wwwroot/upload/erp/sample_dev/";

    @Resource(name = "SampleDevAttachedSheetRepository")
    private SampleDevAttachedSheetRepository _dao;

    @Resource(name = "SampleDevAttachedSheetPictureRepository")
    private SampleDevAttachedSheetPictureRepository _pictureDao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> fetchByHeaderKey(Long key) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        rtnValue.put("headerEntity", _dao.fetchByKey(key));
        rtnValue.put("pictureList", _pictureDao.fetchAll(key));

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> filters) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) ((Integer) filters.get("Limit")) * (((Integer) filters.get("PageIndex")) - 1);
        filters.put("Offset", offset);
        //order_by = string.IsNullOrEmpty( order_by ) ? "" : order_by;

        List<SampleDevAttachedSheetEntity> itemList = _dao.fetchForPage(filters);
        long total = _dao.fetchForCount(filters);

        Paginator<SampleDevAttachedSheetEntity> paginator = new Paginator<>(itemList, offset, (Integer) filters.get("Limit"), total);

        rtnValue.put("TotalRecordCount", paginator.getTotal());
        rtnValue.put("TotalPages", paginator.getTotalPages());
        rtnValue.put("PageIndex", paginator.getPageIndex());
        rtnValue.put("DataList", paginator.getItemList());
        rtnValue.put("Success", true);

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    @WriteLog(modelName = "贸易信息→开发样品跟单表", typeOfOperated = "增加")
    public Long insert(SampleDevAttachedSheetEntity instance) throws Exception {
        return  _dao.insert(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    @WriteLog(modelName = "贸易信息→开发样品跟单表", typeOfOperated = "更新")
    public Integer update(SampleDevAttachedSheetEntity instance) throws Exception {
        return _dao.update(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    @WriteLog(modelName = "贸易信息→开发样品跟单表", typeOfOperated = "删除")
    public Integer delete(SampleDevAttachedSheetEntity instance) throws Exception {
        Integer rowsAffected = _dao.delete(instance.getHeaderId());
        if (rowsAffected > 0) {
            String dir = uploadPath + instance.getHeaderId();
            DeleteDirectoryAndFile.deleteDirectory(dir);
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @WriteLog(modelName = "贸易信息→开发样品跟单表", typeOfOperated = "审核")
    @Override
    public Integer doCheck(SampleDevAttachedSheetEntity instance) throws Exception {
        return _dao.doCheck(instance);
    }
}
